<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
      class="form-search"
    >
      <SearchTool
        :code="{
          factory: 'factory',
          workshop: 'workSection',
          line: 'pdcLine',
          section: 'processSection',
        }"
        :titles="{
          factory: '工厂',
          workshop: '工段',
          line: '产线',
          section: '工艺段',
        }"
        v-model:factory="queryParams.factory"
        v-model:workshop="queryParams.workSection"
        v-model:line="queryParams.pdcLine"
        v-model:section="queryParams.processSection"
      ></SearchTool>
      <el-form-item label="线边仓" prop="storehouseCode">
        <StoreSelect
          v-model="queryParams.storehouseCode"
          :line="queryParams.pdcLine"
          :section="queryParams.processSection"
          placeholder="请选择线边仓"
        ></StoreSelect>
      </el-form-item>
      <el-form-item label="盘点主题" prop="inventoryName">
        <el-input
          v-model="queryParams.inventoryName"
          placeholder="请输入盘点编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="盘点日期" prop="inventoryDate">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="dict in inventory_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['warehouse:inventory:add']"
        >
          新增
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:inventory:remove']"
        >
          删除
        </el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="inventoryList"
      @selection-change="handleSelectionChange"
      border
      maxHeight="500"
      show-overflow-tooltip
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="盘点编号" align="center" prop="inventoryCode">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">
            {{ scope.row.inventoryCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="盘点主题" align="center" prop="inventoryName" />

      <el-table-column label="线边仓" align="center" prop="storehouseName" />
      <el-table-column label="盘点日期" align="center" prop="inventoryDate" />
      <el-table-column label="盘点完成时间" align="center" prop="inventoryDoneDate" width="180">
        <template #default="scope">
          <span>
            {{
              scope.row.inventoryDoneDate
                ? parseTime(scope.row.inventoryDoneDate, "{y}-{m}-{d}")
                : "-"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="盘点人" align="center" prop="inventoryUserName" />
      <el-table-column label="工厂" align="center" prop="factoryName" />
      <el-table-column label="工段" align="center" prop="workShopName" />
      <el-table-column label="产线" align="center" prop="lineName" />
      <el-table-column label="工艺段" align="center" prop="sectionName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="inventory_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fix="right"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 'SUBMIT_TEMPORARY_STORAGE'"
            link
            type="primary"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:inventory:edit']"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.row.status === 'SUBMIT_TEMPORARY_STORAGE'"
            link
            type="primary"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:inventory:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-model="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="盘点编号" prop="inventoryCode">
          <el-input v-model="form.inventoryCode" placeholder="请输入盘点编号" />
        </el-form-item>
        <el-form-item label="主题" prop="inventoryName">
          <el-input v-model="form.inventoryName" placeholder="请输入主题" />
        </el-form-item>
        <el-form-item label="线边仓code" prop="storehouseCode">
          <el-input v-model="form.storehouseCode" placeholder="请输入线边仓code" />
        </el-form-item>
        <el-form-item label="线边仓名称" prop="storehouseName">
          <el-input v-model="form.storehouseName" placeholder="请输入线边仓名称" />
        </el-form-item>
        <el-form-item label="盘点日期" prop="inventoryDate">
          <el-input v-model="form.inventoryDate" placeholder="请输入盘点日期" />
        </el-form-item>
        <el-form-item label="盘点完成时间" prop="inventoryDoneDate">
          <el-date-picker
            clearable
            v-model="form.inventoryDoneDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择盘点完成时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="盘点人name" prop="inventoryUserName">
          <el-input v-model="form.inventoryUserName" placeholder="请输入盘点人name" />
        </el-form-item>
        <el-form-item label="工厂" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入工厂" />
        </el-form-item>
        <el-form-item label="工段" prop="workSection">
          <el-input v-model="form.workSection" placeholder="请输入工段" />
        </el-form-item>
        <el-form-item label="产线" prop="pdcLine">
          <el-input v-model="form.pdcLine" placeholder="请输入产线" />
        </el-form-item>
        <el-form-item label="工艺段" prop="processSection">
          <el-input v-model="form.processSection" placeholder="请输入工艺段" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInventory,
  addInventory,
  updateInventory,
  delInventory,
} from "@/api/warehouse/inventory";
import DetailData from "./data";
export default {
  name: "Inventory",
  data() {
    return {
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryCode: null,
        inventoryName: null,
        storehouseCode: null,
        storehouseName: null,
        inventoryDate: null,
        inventoryDoneDate: null,
        inventoryUserName: null,
        factory: null,
        workSection: null,
        pdcLine: null,
        processSection: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inventoryCode: [{ required: true, message: "盘点编号不能为空", trigger: "blur" }],
        storehouseCode: [{ required: true, message: "线边仓不能为空", trigger: "blur" }],
        storehouseName: [{ required: true, message: "线边仓名称不能为空", trigger: "blur" }],
        inventoryDate: [{ required: true, message: "盘点日期不能为空", trigger: "blur" }],
        inventoryDoneDate: [{ required: true, message: "盘点完成时间不能为空", trigger: "blur" }],
        inventoryUserName: [{ required: true, message: "盘点人不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        createBy: [{ required: true, message: "创建人不能为空", trigger: "blur" }],
        createTime: [{ required: true, message: "创建日期不能为空", trigger: "blur" }],
      },
      inventory_status: [],
      selectData: [],
    };
  },
  created() {
    this.getList();
    const { inventory_status } = this.useDict("inventory_status");
    this.inventory_status = inventory_status;
  },
  methods: {
    /** 查询盘点列表 */
    getList() {
      this.loading = true;
      console.log(this.dateRange);
      if (this.dateRange.length) {
        this.queryParams.startTime = this.dateRange[0] + " 00:00:00";
        this.queryParams.endTime = this.dateRange[1] + " 23:59:59";
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      listInventory(this.addDateRange(this.queryParams)).then((response) => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inventoryCode: null,
        inventoryName: null,
        storehouseCode: null,
        storehouseName: null,
        inventoryDate: null,
        inventoryDoneDate: null,
        inventoryUserName: null,
        factory: null,
        workSection: null,
        pdcLine: null,
        processSection: null,
        status: "0",
        remark: null,

        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.dateRange = [];
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selectData = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.subPage(DetailData, { title: "添加盘点" }, () => {
        this.getList();
      });
    },

    handleDetail(row) {
      this.subPage(DetailData, { title: "盘点详情", data: row }, () => {
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.subPage(DetailData, { title: "编辑盘点", data: row }, () => {
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(() => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(() => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (!row.id) {
        const deleteData = this.selectData.filter((el) => el.status !== "SUBMIT_TEMPORARY_STORAGE");
        if (deleteData.length) {
          this.$modal.msgError("仅可删除盘点中状态数据");
          return;
        }
      }
      const ids = row.id || this.ids;
      const codes =
        row.inventoryCode ||
        ids
          .map((id) => this.inventoryList.find((item) => item.id === id)?.inventoryCode)
          .join(", ");
      this.$modal
        .confirm("是否确认删除编号为" + codes + "数据项？")
        .then(function () {
          return delInventory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/inventory/export",
        {
          ...this.queryParams,
        },
        `inventory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
